package com.dvdfe8.crochet.controlador;

import com.dvdfe8.crochet.modelo.entity.Amigurumi;
import com.dvdfe8.crochet.modelo.entity.Lana;
import com.dvdfe8.crochet.modelo.service.AmigurumiService;
import com.dvdfe8.crochet.modelo.service.LanaService;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author David
 */
@Controller
@RequestMapping("/amigurumi")
public class AmigurumiController {

    @Autowired
    AmigurumiService amigurumiService;
    
    @Autowired
    LanaService lanaService;

    @RequestMapping("/")
    public String amigurumi(){
        return "redirect:listar";
    }
    
    @RequestMapping("/nuevo")
    public ModelAndView nuevo() {
        ModelAndView mav = new ModelAndView("amigurumi/edit");
        Amigurumi amigurumi = new Amigurumi();
        mav.addObject("amigurumi", amigurumi);
        return mav;
    }

    @RequestMapping("/editar")
    public ModelAndView editar(@RequestParam int id){
        ModelAndView mav = new ModelAndView("amigurumi/edit");
        Amigurumi amigurumi = amigurumiService.getAmigurumiById(id);
        mav.addObject("amigurumi", amigurumi);
        return mav;
    }
    
    @RequestMapping("/crear")
    public String crear(@ModelAttribute Amigurumi amigurumi, BindingResult result) {
        if (result.hasErrors()) {
            return "redirect:nuevo";
        }
        amigurumiService.crear(amigurumi);

        return "redirect:listar";
    }

    @RequestMapping("/listar")
    public ModelAndView listar() {
        ModelAndView mv = new ModelAndView("amigurumi/listado");
        List<Amigurumi> amigurumis = amigurumiService.listarTodos();
        mv.addObject("amigurumis", amigurumis);
        return mv;
    }
    
    @RequestMapping("/mostrar")
    public ModelAndView mostrar(@RequestParam int id){
        ModelAndView mv = new ModelAndView("amigurumi/datos");
        Amigurumi amigurumi = amigurumiService.getAmigurumiById(id);
        mv.addObject("amigurumi", amigurumi);       
        return mv;
    }

    @RequestMapping("/eliminar")
    public String eliminar(@RequestParam int id){
        Amigurumi amigurumi = amigurumiService.getAmigurumiById(id);
        amigurumiService.eliminar(amigurumi);
        return "redirect:../listar";
    }
    
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(
                dateFormat, false));
    }
    
    @RequestMapping("/agregarLanas")
    public ModelAndView agregarLanas(@RequestParam int id){
        Amigurumi amigurumi = amigurumiService.getAmigurumiById(id);
        ModelAndView mav = new ModelAndView("amigurumi/listarLanas");
        ArrayList<Lana> lanas = (ArrayList)lanaService.listarTodos();
        mav.addObject("lanas", lanas);
        mav.addObject("amigurumi", amigurumi);
        return mav;
    }
    
    @RequestMapping("/asignarLanas")
    public String asignarLanas(HttpServletRequest request, HttpServletResponse response){
        String tamano = request.getParameterValues("tam")[0];
        int tam = Integer.parseInt(tamano);
        String ami = request.getParameterValues("amigurumi")[0];
        int amigurumiId = Integer.parseInt(ami);
        Amigurumi amigurumi = amigurumiService.getAmigurumiById(amigurumiId);
        
        for(int i=0; i<tam; i++){
            if(request.getParameter("lana"+i)!=null){
                String lan = request.getParameterValues("lana"+i)[0];
                int lanaId = Integer.parseInt(lan);
                Lana lana = lanaService.getLanaById(lanaId);
                amigurumi.agregarLana(lana);
                amigurumiService.crear(amigurumi);
            }
        }
        return "redirect:listar";
    }
    
}
